package web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.Date;
import java.util.zip.CRC32;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import test.DateTest;
import test.PropertyUtil;
import test.RSAEncryption;

import com.opensymphony.xwork2.ActionSupport;

public class HttpAction extends ActionSupport{
   
	/**
	 * 
	 */
	private static final long serialVersionUID = 4122581409238605360L;
	
	private static final String NO = "NO";
	@SuppressWarnings("unused")
	private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNSSX8/YQoMcgm3RCo/ZXXyNwGIMhEdWu6e7cz"
			+ "dwJK4Rj9Vb2eTM/ChRRtVFGyti3PY+AYgDL/p4ncfgmH0wKTZwm0BpwDfaCE+uwOIBtPo73PctVv"
			+ "pHFjXhLbKqwHDOMTMrRraxpwIv+y16GUME+/Xs+lajeQwwHDcf452xkd9QIDAQAB";   
	private static final String PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAM1JJfz9hCgxyCbdEKj9ldfI3AYg"
			+ "yER1a7p7tzN3AkrhGP1VvZ5Mz8KFFG1UUbK2Lc9j4BiAMv+nidx+CYfTApNnCbQGnAN9oIT67A4g"
			+ "G0+jvc9y1W+kcWNeEtsqrAcM4xMytGtrGnAi/7LXoZQwT79ez6VqN5DDAcNx/jnbGR31AgMBAAEC"
			+ "gYEAnIFkANiWZL5vRZHMHcoW7wgnFlTv2aFbnDStmwLkMIokLx+rDICq9DXZOPDLyF+z1STjuEok"
			+ "l1v3cq50PaTVlIVNgEykHaNE35Pv6NHywXCzlnKOwqfYRW5rFxQcMFUfrelMd9atGGfvmCBmW8ol"
			+ "1hqoS/0sH/Hk/qRkuNHiFRECQQDwVQGZgqXKjL9bM/6yG6nCFjwK4HZhbjeOunS8Ge0F0xo4Qrj5"
			+ "t+W0iPZL91kvEHQG98OlxXO+cv9heu9xrtEnAkEA2qs9nZZp0Kim22w/DMtOuFT3PtvXRKMQjwJ1"
			+ "O7Tbxz3dgqPcxNrzv689f6Rt88Ybhr5U9fsitHrXnADgVouRgwJAD0M2wMr9QGnD1UTuDd2X4E2N"
			+ "LpumqPVY7eWFeFSlKtOHPL9wA9vb6KUyDwz8SvLei3q8oBB0E0o2Ubh+8FdLzQJBAKXGNEwH/+hk"
			+ "Mzsd8R6t8tvj7h0FZUlvpdoFLwM4tXVy/T9RIslKhIfJPoJKEeFBSxT3rkldPo48osBuGaUC4ikC"
			+ "QQCQlsjC4c1tODKOKOkWWriQ+rmS+jtyfk/2zXlv1Fl806lx+SFI5lbCUhWWhZZ8PxyGFbcayG7L"
			+ "0gmj9pR8OoJd"; 

	@Override
	public String execute()throws Exception{
		System.out.println("接收成功！");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
 		response.setContentType("text/xml; charset=utf-8");
		response.setCharacterEncoding("UTF-8");

		InputStream is = request.getInputStream();
		String data = convertStreamToString(is);
		byte bytes[] = RSAEncryption.decryptByPrivateKey(PRIVATE_KEY, data);

		System.out.println(new String(bytes, "utf-8"));
		String lastData = new String(bytes, "utf-8");
		
		String text[] = lastData.split("#CRC#");
		
		CRC32 CRCout =new CRC32();//定义校验对象
		CRCout.update(text[0].getBytes());			
		Long crc=CRCout.getValue();//返回校验值给crc
		System.out.println(text[1].equals(crc.toString()));
		
		if(!text[1].equals(crc.toString())){
	        Writer w = response.getWriter();
	        w.write(NO);
	        w.close();
			
		}else{
		
		File dir = new File(PropertyUtil.getDirectory());
		File file = new File(PropertyUtil.getDirectory() + "android上传文件"
				+ DateTest.dateFormat(new Date()) + ".xml");
		if(!dir.exists()){
			dir.mkdirs();
		}
		if(!file.exists()){
			file.createNewFile();
		}
		FileWriter  fw = null;
		try {
			fw = new FileWriter(file);
		    fw.write(text[0]);
		    fw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	 }
		System.out.println("上传成功！");
		return null;
	}
	
	 public static String convertStreamToString(InputStream is) throws IOException {
		 BufferedReader in = new BufferedReader(new InputStreamReader(is));
		    StringBuffer buffer = new StringBuffer();
		    String line = "";
		    while ((line = in.readLine()) != null){
		      buffer.append(line);
		    }
		    return buffer.toString();
	 }
}
